package com.bnz.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bnz.common.PageUtils;
import com.bnz.common.entity.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.shop.entity.ItemCatEntity;
import com.bnz.shop.service.ItemCatService;



/**
 * 商品类目
 *
 * @author hbxfwf
 * @email 2568783935@qq.com
 * @date 2023-02-16 09:57:43
 */
@RestController
@RequestMapping("shop/itemcat")
public class ItemCatController {
	@Autowired
	private ItemCatService itemCatService;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = itemCatService.queryPage(params);

		return R.ok().put("page", page);
	}

	//查询所有分类
	@GetMapping("/findAll")
	public R findAll() {
		return R.ok().put("list", itemCatService.list());
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		ItemCatEntity itemCat = itemCatService.getById(id);

		return R.ok().put("itemCat", itemCat);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	public R save(@RequestBody ItemCatEntity itemCat) {
		itemCatService.save(itemCat);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PutMapping("/update")
	public R update(@RequestBody ItemCatEntity itemCat) {
		itemCatService.updateById(itemCat);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		itemCatService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}
	// 根据id查询一级分类下面的所有类别
	@GetMapping("/findById/{id}")
	public R findById(@PathVariable String id){
		List<ItemCatEntity> parent_id = itemCatService.list(new QueryWrapper<ItemCatEntity>().eq("parent_id", id));
		return R.ok().put("categories",parent_id);
	}

}
